package com.zls.mapper;

import com.zls.dto.DeveloperDto;
import com.zls.pojo.Developer;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface DeveloperMapper {

    @Results(id = "deveMap",value = {
            @Result(column = "did",property = "did"),
            @Result(column = "dname",property = "dname"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "did",property = "projects",many =   //一对多映射
                    //select 关联查询
            @Many(select ="com.com.zls.mapper.ProjectMapper.selectByDids" ,fetchType = FetchType.EAGER)
            )
    })
    @Select("Select * from developer where status =1")
    List<Developer> findAll();

    @Select("<script>" +
            "select * from developer where status =1" +
            "<if test='queryString!=null'> and dname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Developer> findDeve(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from developer where status =1" +
            "<if test='queryString!=null'> and dname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    //添加
    @Insert("insert into developer (did,dname,updatetime) values(#{did},#{dname},#{updatetime})")
    int add(Developer developer);

    //逻辑删除
    @Update("update developer set status=0 where did=#{did}")
    int deleteDeve(String did);

    //根据did查询
    @ResultMap("deveMap")
    @Select("select * from developer where status =1 and did=#{did}")
    Developer findByDid(String pid);
    //dto方式
    @Select("select * from developer where did =#{did} and status=1")
    DeveloperDto findByDidDto(String did);
    //修改
    @Update("update developer set dname=#{dname},updatetime=#{updatetime} where did=#{did} and status =1")
    int updateDeve(Developer developer);

}
